package cn.edu.zut.cs.Service;

import cn.edu.zut.cs.DAO.PatientDAO;
import cn.edu.zut.cs.DAO.WardDAO;
import cn.edu.zut.cs.Model.Patient;
import cn.edu.zut.cs.Model.Ward;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WPService {
    PatientDAO patientDAO;
    WardDAO wardDAO;

    @Autowired
    public void setPatientDAO(PatientDAO patientDAO) {
        this.patientDAO = patientDAO;
    }
    @Autowired
    public void setWardDAO(WardDAO wardDAO) { this.wardDAO=wardDAO; }

    // ************     单纯病人表的操作     ****************    //
    //插入一个病人
    public void insertPatient(Patient patient){
        patientDAO.insertPatient(patient);
    }
    //依据Pno修改病人信息
    public void modifyPatient(Patient patient){
        patientDAO.modifyPatient(patient);
    }
    //删除病人信息，本质是依据Pno
    public void deleteByPno(String pno){
        patientDAO.deleteByPno(pno);
    }
    //根据Pno查询病人信息
    public Patient queryByPno(String Pno){
        return patientDAO.queryByPno(Pno);
    }
    //删除所有病人
    public void deleteAllPatients(){
        patientDAO.deleteAll();
    }
    //根据姓名查询病人
    public List<Patient> queryByName(String name){
        return patientDAO.queryByName(name);
    }
    //查询一个病房的所有病人信息，根据床位号排序  0.0
    public List<Patient> queryByWard(String wardId) { return patientDAO.queryByWard(wardId); }
    //打印所有病人信息，并排序，优先级：wardId>bedId>pno
    public List<Patient> queryAllPatients(){
        return patientDAO.queryAll();
    }

    //************       单纯病房的操作      ****************   //
    //新添一个病房
    public void insertWard(Ward ward) { wardDAO.insertWard(ward); }
    //修改一个病房信息
    public void modifyWard(Ward ward) { wardDAO.modifyWard(ward); }
    //查询所有病房信息，根据病房号排序
    public List<Ward> queryAllWard() { return wardDAO.queryAll(); }
    //根据病房号查询
    public Ward queryByWardId(String wardId){ return wardDAO.queryByWardId(wardId); }
    //根据病房类型查询
    public List<Ward> queryByType(String type) { return wardDAO.queryByType(type); }
    //删除所有病房信息
    public void deleteAllWard(){ wardDAO.deleteAll(); }
    //根据病房号删除一个病房
    public void deleteByWardId(String wardId){ wardDAO.deleteByWardId(wardId); }

    //************       病房表和病人的联合操作      ****************   //
    //根据病房状态查询有空缺床位的所有病房信息
    public List<Ward> queryAllWPByType(String type){
        List<Ward> wards=wardDAO.queryByType(type);
        for(int i=0;i<wards.size();++i){
            if(wards.get(i).getSum()==4)wards.remove(wards.get(i));
        }
        return wards;
    }
    //插入病人，自动匹配病房号和床位号，不存在空位则返回false，否则返回true
    public boolean insertWP(Patient patient){
        List<Ward> wards=queryAllWPByType(patient.getStatus());
        for(Ward ward: wards){
            if(ward.getSum()<4){
                patient.setWardId(ward.getWardId());
                if(ward.getBed_1().equals("空")){
                    patient.setBedId(1);
                    ward.setBed_1(patient.getPno());
                }
                else if(ward.getBed_2().equals("空")){
                    patient.setBedId(2);
                    ward.setBed_2(patient.getPno());
                }
                else if(ward.getBed_3().equals("空")){
                    patient.setBedId(3);
                    ward.setBed_3(patient.getPno());
                }
                else {
                    patient.setBedId(4);
                    ward.setBed_4(patient.getPno());
                }
                ward.setSum(ward.getSum()+1);
                patientDAO.insertPatient(patient);
                wardDAO.modifyWard(ward);
                return true;
            }
        }
        return false;
    }
    //插入病人时选择病房
    public boolean insertWP(Patient patient,String wardId){
        Ward ward=wardDAO.queryByWardId(wardId);
            if(ward.getSum()<4){
                if(ward.getBed_1().equals("空")){
                    patient.setBedId(1);
                    ward.setBed_1(patient.getPno());
                }
                else if(ward.getBed_2().equals("空")){
                    patient.setBedId(2);
                    ward.setBed_2(patient.getPno());
                }
                else if(ward.getBed_3().equals("空")){
                    patient.setBedId(3);
                    ward.setBed_3(patient.getPno());
                }
                else {
                    patient.setBedId(4);
                    ward.setBed_4(patient.getPno());
                }
                ward.setSum(ward.getSum()+1);
                patientDAO.insertPatient(patient);
                wardDAO.modifyWard(ward);
                return true;
            }
        return false;
    }
    //插入病人时选择病房和床号
    public boolean modify_insert(Patient patient){
        Ward ward=wardDAO.queryByWardId(patient.getWardId());
        int bedId=patient.getBedId();
            if(ward.getBedId(bedId).equals("空")) {
                ward.setBedId(bedId,patient.getPno());
                ward.setSum(ward.getSum() + 1);
                patientDAO.insertPatient(patient);
                wardDAO.modifyWard(ward);
                return true;
            }
        return false;
    }
    //删除病人，连带着病房表的也要删除
    public void deleteWP(String pno){
        Patient patient=patientDAO.queryByPno(pno);
        patientDAO.deleteByPno(pno);
        Ward ward=wardDAO.queryByWardId(patient.getWardId());
        if(ward.getBed_1()==pno) ward.setBed_1("空");
        else if(ward.getBed_2()==pno) ward.setBed_2("空");
        else if(ward.getBed_3()==pno) ward.setBed_3("空");
        else  ward.setBed_4("空");
        ward.setSum(ward.getSum()-1);
        wardDAO.modifyWard(ward);
    }
    //修改病人信息，连同病房信息一并修改
    public void modifyWP(Patient prePatient,Patient nowPatient){
        deleteWP(prePatient.getPno());
        if(!modify_insert(nowPatient)){
            modify_insert(prePatient);
        }
    }
}
